package com.vimi8.ebb.auth.configuration;

import com.vimi8.ebb.auth.service.DBClientDetailsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;

/**
 * Created by xuybin on 2016/9/8.
 */
public class ClientDetailsServiceImpl implements ClientDetailsService {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DBClientDetailsService dbClientDetailsService;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ClientDetails clientDetails= null;
        try {
            clientDetails=dbClientDetailsService.getClientDetailsByClientId(clientId);
        }catch (Exception ex){
            logger.error(ex.getLocalizedMessage());
        }
        if (clientDetails == null) {
            throw new ClientRegistrationException(String.format("Client with id %s not found", clientId));
        }
        logger.warn("loadClientByClientId clientDetails:"+clientDetails.toString());
        return clientDetails;
    }
}
